package com.bibrApp.difu.actvity.record_mvp;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.others.HelpActivity;
import com.bibrApp.difu.actvity.others.RecordDetailActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.UIUtils;
import com.bibrApp.difu.widget.HorizontalDividerItemDecoration;


import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RecordActivity extends BaseActivity<RecordPresenter> implements IRecordView {


    @BindView(R.id.tv_chongzhi)
    TextView tvChongzhi;
    @BindView(R.id.tv_tixian)
    TextView tvTixian;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_select_1)
    TextView tvSelect1;
    @BindView(R.id.tv_select_2)
    TextView tvSelect2;
    @BindView(R.id.tv_chong_line)
    View chongLine;
    @BindView(R.id.tv_ti_line)
    View tiLine;

    int type = 0;
    int pageSize = 10;
    int pageNum = 1;
    String coinEuit = "";
    private RecordAdapter adapter;
    private PopupWindow popupWindow;
    private PopupWindow popupWindow1;


    @Override
    protected void initPresenter() {
        presenter = new RecordPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_record;
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void onDataResult(List<RecordBean> list) {
        if (pageNum == 1 && (list == null || list.size() == 0) && adapter.getEmptyViewCount() == 0) {
            adapter.setEmptyView(View.inflate(this, R.layout.empty_order, null));
            return;
        }
        if (list.size() != pageSize) {
            adapter.loadMoreEnd();
        } else {
            adapter.loadMoreComplete();
        }
        adapter.addData(list);
    }

    @Override
    public void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(Color.parseColor("#F0F0F0"))
                .sizeResId(R.dimen.dp_10)
                .build());
        adapter = new RecordAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(RecordActivity.this, RecordDetailActivity.class);
            intent.putExtra("bean", adapter.getItem(position));
            intent.putExtra("type", type);
            startActivity(intent);
        });
        adapter.setOnLoadMoreListener(() -> {
            pageNum++;
            presenter.getDatas(app.getuId(), type, coinEuit, pageNum, pageSize);
        }, recyclerView);

        chongzhiClick();
    }

    @OnClick(R.id.bt_back)
    public void backClick() {
        finish();
    }

    @OnClick(R.id.bt_help)
    public void helpClick() {
        startActivity(new Intent(this, HelpActivity.class));
    }

    @OnClick(R.id.tv_chongzhi)
    public void chongzhiClick() {
        if (type == 1) {
            return;
        }
        type = 1;
        tvChongzhi.setTextColor(getResources().getColor(R.color.blue_e4));
        tvTixian.setTextColor(getResources().getColor(R.color.black_a7));
        chongLine.setVisibility(View.VISIBLE);
        tiLine.setVisibility(View.INVISIBLE);

        pageNum = 1;
        coinEuit = "";

        presenter.getDatas(app.getuId(), type, coinEuit, pageNum, pageSize);
    }

    @OnClick(R.id.tv_tixian)
    public void tixianClick() {
        if (type == 2) {
            return;
        }
        type = 2;
        tvTixian.setTextColor(getResources().getColor(R.color.blue_e4));
        tvChongzhi.setTextColor(getResources().getColor(R.color.black_a7));
        tiLine.setVisibility(View.VISIBLE);
        chongLine.setVisibility(View.INVISIBLE);


        pageNum = 1;
        coinEuit = "";

        presenter.getDatas(app.getuId(), type, coinEuit, pageNum, pageSize);
    }

    @OnClick(R.id.tv_select_1)
    public void select1Click() {
        View layout = View.inflate(this, R.layout.pop_coin_type, null);

        layout.findViewById(R.id.tv_all).setOnClickListener(view -> {
            tvSelect1.setText("全部");
            coinEuit = "";
            pageNum = 1;
            presenter.getDatas(app.getuId(), type, coinEuit, pageNum, pageSize);
            popupWindow.dismiss();
        });
        layout.findViewById(R.id.tv_btc).setOnClickListener(view -> {
            tvSelect1.setText("BTC");
            coinEuit = "BTC";
            pageNum = 1;
            presenter.getDatas(app.getuId(), type, coinEuit, pageNum, pageSize);
            popupWindow.dismiss();
        });
        layout.findViewById(R.id.tv_eth).setOnClickListener(view -> {
            tvSelect1.setText("ETH");
            coinEuit = "ETH";
            pageNum = 1;
            presenter.getDatas(app.getuId(), type, coinEuit, pageNum, pageSize);
            popupWindow.dismiss();
        });
        layout.findViewById(R.id.tv_usdt).setOnClickListener(view -> {
            tvSelect1.setText("USDT");
            coinEuit = "USDT";
            pageNum = 1;
            presenter.getDatas(app.getuId(), type, coinEuit, pageNum, pageSize);
            popupWindow.dismiss();
        });

        popupWindow = new PopupWindow(layout, UIUtils.dpToPixels(this, 68), -2, true);
        popupWindow.setFocusable(true);

        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(tvSelect1, 0, 0, Gravity.CENTER);
    }

    @OnClick(R.id.tv_select_2)
    public void select2Click() {
        View layout = View.inflate(this, R.layout.pop_coin_type, null);

        layout.findViewById(R.id.tv_all).setOnClickListener(view -> {
            tvSelect2.setText("全部");
            popupWindow1.dismiss();
        });
        TextView textView1 = layout.findViewById(R.id.tv_btc);
        TextView textView2 = layout.findViewById(R.id.tv_eth);
        TextView textView3 = layout.findViewById(R.id.tv_usdt);
        textView1.setText(type == 1 ? "普通充币" : "普通提币");
        textView2.setText(type == 1 ? "多签充币" : "多签提币");
        textView3.setText(type == 1 ? "合约充币" : "合约提币");
        textView1.setOnClickListener(view -> {
            tvSelect2.setText(type == 1 ? "普通充币" : "普通提币");
            popupWindow1.dismiss();
        });
        textView2.setOnClickListener(view -> {
            tvSelect2.setText(type == 1 ? "多签充币" : "多签提币");
            popupWindow1.dismiss();
        });
        textView3.setOnClickListener(view -> {
            tvSelect2.setText(type == 1 ? "合约充币" : "合约提币");
            popupWindow1.dismiss();
        });

        popupWindow1 = new PopupWindow(layout, UIUtils.dpToPixels(this, 84), -2, true);
        popupWindow1.setFocusable(true);

        popupWindow1.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        popupWindow1.setOutsideTouchable(true);
        popupWindow1.showAsDropDown(tvSelect2, 0, 0, Gravity.CENTER);
    }
}
